//
//  OnlineShoppingInfoViewController.m
//  NewApp
//
//  Created by  on 12-7-9.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "OnlineShoppingInfoViewController.h"
#import "OnlineShoppingSearchResultViewController.h"
#import "ShoppingCarViewController.h"

@interface OnlineShoppingInfoViewController ()

@end

@implementation OnlineShoppingInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"返回";
}

- (void) dealloc {
    [super dealloc];
}

- (void) loadInlineInfos {
    [self.engine onlineShoppingInfosWithID:self.ID];
}

- (void) buildUserInterface {
    [super buildUserInterface];
    
    [self.toolView.confirmButton setTitle:@"加入購物" forState:UIControlStateNormal];
    self.toolView.confirmButton.titleLabel.font = [UIFont systemFontOfSize:13.f];
}

- (void) searchButtonDidPressed {
    if (_searchBar.keywordTextfield.text.length == 0) 
        return;
    self.title = @"返回";
    OnlineShoppingSearchResultViewController *viewCtrl = [[OnlineShoppingSearchResultViewController alloc] initWithID:nil keyword:_searchBar.keywordTextfield.text];
    self.title = @"返回";
    [self.navigationController pushViewController:viewCtrl animated:YES];
    [viewCtrl release];
}

#pragma mark -
#pragma mark - Event handlers

- (void) toolViewConfirmButtonPressed {
    
    BOOL bValidated = YES;
    
    if (self.toolView.countTextfield.text.length == 0) {
        bValidated = NO;
    }
    
    if ([self.toolView.countTextfield.text intValue] == 0) {
        bValidated = NO;
    }
    
    if (!bValidated) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" 
                                                        message:@"請輸入正確的整數值" delegate:nil
                                              cancelButtonTitle:@"確定" 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
    
    
    NSMutableString *shopcarList = [[NSMutableString alloc] init];
    [shopcarList appendString:([[NSUserDefaults standardUserDefaults] objectForKey:kShopCarListKey] == nil) ? @"" : [[NSUserDefaults standardUserDefaults] objectForKey:kShopCarListKey]];
    
    if (shopcarList.length == 0) {
        [shopcarList appendFormat:@"%@,%@",self.ID,self.toolView.countTextfield.text];
    }else {
        [shopcarList appendFormat:@"x%@,%@",self.ID,self.toolView.countTextfield.text];
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:shopcarList forKey:kShopCarListKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    ShoppingCarViewController *vctrl = [[ShoppingCarViewController alloc] init];
    vctrl.pageType = PAGE_TYPE_SHOP;
    vctrl.selectProducts = [shopcarList retain];
    self.title = @"返回";
    [self.navigationController pushViewController:vctrl animated:YES];
    [vctrl release];
}



@end
